package com.krt.facenettydemo.utils;

import android.graphics.Bitmap;
import android.os.AsyncTask;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import static com.krt.facenettydemo.base.Constants.CachePath;

/**
 * @author hyj
 * @time 2019/9/11 11:29
 * @class describe
 */
public class SaveLocalFace extends AsyncTask {

    private Bitmap bitmap;
    private String fn;

    public SaveLocalFace(Bitmap bitmap, String fn) {
        this.bitmap = bitmap;
        this.fn = fn;
    }

    @Override
    protected Object doInBackground(Object[] objects) {

        if (bitmap == null) return null;

        String faceName = "face" + fn + ".png";
        File ImageFile = new File(CachePath, faceName);
        try {
            if (!ImageFile.exists()) {
                ImageFile.getParentFile().mkdirs();
                ImageFile.createNewFile();
            }
            FileOutputStream out = new FileOutputStream(ImageFile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return ImageFile.toString();
    }
}
